package com.lanou.bookstore.user.web.servlet;

import com.lanou.bookstore.cart.domain.CartItem;
import com.lanou.bookstore.user.domain.User;
import com.lanou.bookstore.user.service.UserService;
import com.lanou.bookstore.user.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {

    UserService us = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String inputName=(String) req.getParameter("input");

        User user = new User();
        Integer num = (int) (Math.random()*1000 + 1000);
        Integer num2 = (int) (Math.random()*1000 + 1000);
        user.setUid(num.toString());
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setCode(num2.toString());

        UserService userSer = new UserServiceImpl();
        HttpSession session = req.getSession();

        if(inputName.equals("注册")) {

            try {
                boolean b = userSer.registCheck(user);
                if (b) {

                    us.add(user);


//                    List<CartItem> cartList = new ArrayList<>();
//                    //todo
//                    String cart = username.concat("cartList");
//                    session.setAttribute(cart, cartList);
//                    session.setMaxInactiveInterval(60 * 60 * 24);
//
//                    // 把sessionId保存到cookie中
//                    Cookie cookie = new Cookie("JSESSIONID", session.getId());
//                    cookie.setMaxAge(60 * 60 * 24);
//                    resp.addCookie(cookie);

                    req.setAttribute("msg", user);
                    req.getRequestDispatcher("/jsps/msg.jsp").forward(req, resp);
                } else {
                    req.setAttribute("msg", user);
                    req.getRequestDispatcher("/jsps/user/regist.jsp").forward(req, resp);
                }
            } catch (Exception e) {
                e.printStackTrace();
                String message = e.toString();
                message=message.replaceAll("[A-Za-z]|[.]|[;]|[:]","");
                req.setAttribute("errMessage",message);
                req.getRequestDispatcher("/jsps/user/regist.jsp").forward(req, resp);
            }

        }

        if(inputName.equals("登录")) {
            if (userSer.loginCheck(user)) {

                // 在session里保存用户的信息
                session.setAttribute("uname", username);
                // 1天内,就算浏览器关闭,也可以免登录
                session.setMaxInactiveInterval(60 * 60 * 24);

                // 把sessionId保存到cookie中
                Cookie cookie = new Cookie("JSESSIONID", session.getId());
                cookie.setMaxAge(60 * 60 * 24);
                resp.addCookie(cookie);


                // 把购物车放到session中
                List<CartItem> cartList = new ArrayList<>();
                session.setAttribute("cartList", cartList);

//                req.getSession().setAttribute("uname", username);
                req.getRequestDispatcher("/index.jsp").forward(req, resp);



            }else {
                req.getRequestDispatcher("/jsps/user/login.jsp").forward(req, resp);
            }
        }


        if(inputName.equals("进入后台")) {
            if (userSer.loginCheck(user)) {

                // 在session里保存用户的信息
                session.setAttribute("adminName", username);
//                // 2分钟内,就算浏览器关闭,也可以免登录
//                session.setMaxInactiveInterval(60 * 60 *24);
//
//                // 把sessionId保存到cookie中
//                Cookie cookie = new Cookie("JSESSIONID", session.getId());
//                cookie.setMaxAge(60 * 60 * 24);
//                resp.addCookie(cookie);


                req.getRequestDispatcher("/adminjsps/admin/main.jsp").forward(req, resp);



            }else {
                req.getRequestDispatcher("/adminjsps/login.jsp").forward(req, resp);
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        doGet(req, resp);
    }
}
